package com.joinus.server.dao;

import java.sql.ResultSet;

import com.joinus.server.constant.EntityConstant;

/**
 * 用户参加活动Dao<br>
 * @author henrybit
 * @version 1.0
 * @since 2013-4-18
 */
public class UserMeetingJoinDao extends BaseDao {
	/**活动Dao实例*/
	private final static UserMeetingJoinDao userMeetingJoinDao = new UserMeetingJoinDao();
	/**插入SQL前缀*/
	private final static String INSERT_NEW_USER_MEETING_JOIN = "insert into user_meeting_join (id,uid,meetingId,status) values ";
	
	private UserMeetingJoinDao() {
	}
	
	/**
	 * 获取UserMeetingJoinDao实例<br>
	 * @return userMeetingJoinDao
	 */
	public static UserMeetingJoinDao getInstance() {
		return userMeetingJoinDao;
	}
	
	/**
	 * 创建一个新的用户与活动关系<br>
	 * @param uid 用户ID
	 * @param meetingId 活动ID
	 * @return long-记录主键ID
	 */
	public long createNew(String uid, long meetingId) {
		return this.createNew(uid, meetingId, EntityConstant.USERMEETINGJOIN_STATUS_ENABLE);
	}
	
	/**
	 * 创建一个新的用户与活动关系<br>
	 * @param uid 用户ID
	 * @param meetingId 活动ID
	 * @param status 活动状态(1-有效,-1-无效)
	 * @return long-记录主键ID
	 */
	public long createNew(String uid, long meetingId, int status) {
		StringBuilder sql = new StringBuilder(INSERT_NEW_USER_MEETING_JOIN);
		//id auto increatement by db
		sql.append("(null");
		//uid
		sql.append(",").append(uid);
		//meetingId
		sql.append(",").append(meetingId);
		//status
		sql.append(",").append(status);
		sql.append(")");
		
		ResultSet rs = super.insert(sql.toString());
		long id = -1L;
		try {
			rs.next();
			id = rs.getLong(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
}
